<template>
	<page-meta>
		<navigation-bar :title="nbTitle" :subtitle-color="nbFrontColor" :loading="nbLoading" :front-color="nbFrontColor"
			:background-color="nbBackgroundColor" />
	</page-meta>
	<view class="">

		<view class="example">
			<uni-forms ref="form" :model="user" labelWidth="80px">
				<uni-forms-item label="真实姓名" name="userName">
					<uni-easyinput v-model="user.userName" placeholder="请输入真实姓名" />
				</uni-forms-item>
				<uni-forms-item label="用户昵称" name="nickName">
					<uni-easyinput v-model="user.nickName" placeholder="请输入昵称" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="password">
					<uni-easyinput type="password" v-model="user.password" placeholder="请输入验证码" />
				</uni-forms-item>

			</uni-forms>


			<view class="button-group">
				<button class="btn" @click="submit">提交</button>
			</view>
		</view>
		<view class="footer">
			<view class="">
				<text>
					注册即代表同意
				</text>
				<text @click="handleUserAgrement" style="text-decoration: underline; ">《花季护航用户使用协议》</text>
			</view>
		</view>


	</view>
</template>
<script>
	import {
		getInfo
	}
	from '@/api/register.js'
	export default {
		data() {
			return {
				nbTitle: '注册',
				nbLoading: false,
				nbFrontColor: '#ffffff',
				nbBackgroundColor: '#f7cb50',
				globalConfig: getApp().globalData.config,
				range: [],
				user: {
					userName: "",
					nickName: "",
					password: "",
				},
				rules: {
					userName: {
						rules: [{
							required: true,
							errorMessage: '真实姓名不能为空'
						}]
					},
					nickName: {
						rules: [{
							required: true,
							errorMessage: '用户昵称不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}, {
							pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/,
							errorMessage: ' 至少8-16个字符，至少1个大写字母，1个小写字母和1个数字'
						}]
					},
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			submit(ref) {
				this.$refs.form.validate().then(res => {

					getInfo(this.user).then(response => {
						this.$modal.msgSuccess("注册成功")
						setTimeout(() => {
							uni.navigateBack();
						}, 2000)
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.btn {
		width: 720rpx;
		height: 70rpx;
		background-color: #f7cb50;
		color: #ffffff;
		font-size: 24rpx;
		line-height: 60rpx;
	}

	.footer {
		margin-top: 20rpx;
		display: flex;
		justify-content: space-around;
		color: #676767;
	}
</style>
